package com.ton.utils;

import com.ton.config.Config;

import java.math.BigDecimal;

public class Compute {

    public static String amountToMoneyStr(Long amount) {
        return String.valueOf(amountToMoney(amount));
    }

    public static double amountToMoney(Long amount) {
        return new BigDecimal(amount)
                .divide(new BigDecimal(Config.getAmountPointNumber()), 2, BigDecimal.ROUND_DOWN).doubleValue();
    }

    public static long moneyToAmount(double acctMoney) {
        BigDecimal bignum1 = new BigDecimal(acctMoney);
        BigDecimal bignum2 = new BigDecimal(Config.getAmountPointNumber());
        return bignum1.multiply(bignum2).setScale(2, BigDecimal.ROUND_HALF_UP).longValue();
    }

    public static double add(double num1, double num2) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);

        return bignum1.add(bignum2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double add(double num1, double num2, int points) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);

        return bignum1.add(bignum2).setScale(points, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double addRoundDown(double num1, double num2, int points) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);

        return bignum1.add(bignum2).setScale(points, BigDecimal.ROUND_DOWN).doubleValue();
    }

    public static double subtract(double num1, double num2) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);
        return bignum1.subtract(bignum2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double subtract(double num1, double num2, int points) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);
        return bignum1.subtract(bignum2).setScale(points, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double multiply(double num1, double num2) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);
        return bignum1.multiply(bignum2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double multiply(double num1, double num2, int points) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);
        return bignum1.multiply(bignum2).setScale(points, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double divide(double num1, double num2) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);
        return bignum1.divide(bignum2, 2, BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static double divide(double num1, double num2, int points) {
        BigDecimal bignum1 = new BigDecimal(num1);
        BigDecimal bignum2 = new BigDecimal(num2);
        return bignum1.divide(bignum2, points, BigDecimal.ROUND_HALF_UP).setScale(points, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
